﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SqlOperate;
using System.Data.SqlClient;
using System.Text;
using UserInfo;

public partial class Search_Quotation : Base.BasePage
{
    public string _headStr = "";
    string ThisUrl = "";
    public string minoStr = "";
    public string revUser = "";
    public string strPurchaseID = "";
    public int userID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        ThisUrl = Request.Url.ToString();
        if (CheckCookies())
        {
            int intPurchaseID = 0;
            if (Request.QueryString["cpid"] != null && Request.QueryString["cpid"] != "")
            {
                strPurchaseID = Request.QueryString["cpid"].ToString();
            }
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                strPurchaseID = Request.QueryString["id"].ToString();
            }
            if (Request.QueryString["userID"] != null && Request.QueryString["userID"] != "")
            {
                userID = int.Parse(Request.QueryString["userID"].ToString());
            }

            if (strPurchaseID != "")
            {
                if (!Base.Common.CheckNum(strPurchaseID))
                {
                    Base.BasePage.ShowMessage("不存在该求购信息！", "default.aspx");
                    return;
                }
                else
                {
                    intPurchaseID = int.Parse(strPurchaseID);
                    revUser = intPurchaseID.ToString();
                }
                bind();
            }
            else
            {
                if (userID != 0)
                {
                    bind();
                }
            }
        }
        else
        {
            Response.Redirect("" + Base.BasePage.Config.WebURL + "/login/login.aspx?done=" + ThisUrl);
        }
        if (!IsPostBack)
        {
            MinoHeader();
        }
    }

    /// <summary>
    ///  绑定数据信息
    /// </summary>
    private void bind()
    {
        DataTable dt = GetTable();
        //revUser = dt.Rows[0]["ID"].ToString();
        foreach (DataRow dr in dt.Rows)
        {
            revUser = "<a href=\""+Base.BasePage.Config.WebURL+"shops/index/" + dt.Rows[0]["LoginName"] +Base.BasePage.Config.Rewrite+ "\" target=\"_blank\">" + dt.Rows[0]["CompanyName"].ToString() + "</a>";
            this.hidUserID.Value = dt.Rows[0]["UserID"].ToString();
        }

        if (userID != 0)
        {
            TUserInfo ruinfo = new RUserInfo().GateItem(userID);
            revUser = "<a href=\"" + Base.BasePage.Config.WebURL + "shops/index/" + ruinfo.LoginName + Base.BasePage.Config.Rewrite + "\" target=\"_blank\">" + ruinfo.CompanyName + "</a>";
            this.hidUserID.Value = userID.ToString();
        }
    }


    /// <summary>
    /// 取得比较数据
    /// </summary>
    /// <returns></returns>
    private DataTable GetTable()
    {
        string str = "";
        if (Request.QueryString["cpid"] != null && Request.QueryString["cpid"] != "")
        {
            str = "where  ID=" + Base.GetRequest.GetQueryStringValue("cpid");
        }
        if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
        {
            str = "where  ID=" + Base.GetRequest.GetQueryStringValue("id");
        }
        SqlParameter[] param = new SqlParameter[] 
        {
            new SqlParameter("@strwhere", str),
            new SqlParameter("@strtableName", " C_Purchase left join U_UserInfo  on C_Purchase.UserID=U_UserInfo.UserID"), 
            new SqlParameter("@strOrder", " order by AddDate  desc,ID desc") 
        };
        return SqlHelper.ExecuteTable(CommandType.StoredProcedure, "SP_SelectByWhere", param);

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string Restr = "";
            Message.TMessage tmg = new Message.TMessage();
            if (Base.BasePage.GetCookie("UserID") != null)
            {
                tmg.SendUserID = Convert.ToInt32(Base.BasePage.GetCookie("UserID"));   //发送者
            }
            else
            {
                Response.Redirect("login/login.aspx");
            }
            if (txtTitle.Text.Trim().Equals(string.Empty))
                Restr += "询价标题、";
            else
                tmg.Title = this.txtTitle.Text.Trim();                   //留言标题
            if (txtInfo.Value.Trim().Equals(string.Empty))
                Restr += "补充信息、";
            else
                tmg.Info = this.txtInfo.Value.Trim();                   //留言内容
            tmg.Date = DateTime.Now;                                    //留言时间
            string strParama = "";
            strParama = this.hidUserID.Value.ToString();                 //接收者
            if (strParama != "")
            {
                if (strParama == GetCookie("UserID"))
                {
                    ShowMessage("对不起，自己不能给自己留言！");
                    return;
                }
            }
            else
                Response.Redirect("../default.aspx", true);
            tmg.Nature = "Purchase";                                     //关于哪方面的留言

            Message.RMessage rmg = new Message.RMessage();
            if (Restr != "")
                Restr = Restr.TrimEnd('、');                              //关于哪方面的留言
            if (Restr.Equals(string.Empty))
            {
                rmg.Insert(tmg, strParama);
                ShowMessage("发送成功！", "../default.aspx");
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = Restr + "填写有误！";
            }
        }
        catch
        {
            ShowMessage("发送失败！请重新发送");
        }
    }
    /// <summary>
    /// 头部
    /// </summary>
    /// <param name="UserID">id</param>
    /// <returns></returns>
    private void MinoHeader()
    {
        
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.Append("<div class=\"top\"><div class=\"cot\"><ul class=\"toplt\">");
        strBuilder.Append("<li><a title=\"设为首页\" href=\""+Base.BasePage.Config.WebURL+"#\" onClick=\"this.style.behavior='url(#default#homepage)';this.setHomePage('"+Base.BasePage.Config.WebURL+"');\"><img alt=\"设为首页\" src=\"../images/home.gif\" />设为首页</a></li>");
        strBuilder.Append("<li><a title=\"收藏本站\" href=\"#\"><img alt=\"收藏本站\" src=\"../images/favorites.gif\" />收藏本站</a></li>");
        strBuilder.Append("<li><a title=\"订阅本站\" href=\"#\"><img alt=\"收藏本站\" src=\"../images/rss.gif\" />订阅本站</a></li>");
        strBuilder.Append("</ul>");
        if (Base.BasePage.CheckCookies())
        {
            strBuilder.Append("<p class=\"toprt\">");
            strBuilder.Append("<span class=\"f_c00\">" + Base.GetRequest.GetCookie("LoginName") + "</span>");
            strBuilder.Append("您好，欢迎您的光临[<a title=\"提交企业信息\" href=\"" + Base.BasePage.Config.WebURL + "member/index.aspx\" class=\"l_c00\">提交企业信息</a>][<a title=\"退出\" href=\"#\" onclick=\"delCookie('LoginName')\" class=\"l_c00\">退出</a>]</p>");
        }
        else
        {
            strBuilder.Append("<p class=\"toprt\">您好，欢迎您的光临");
            strBuilder.Append("[<a title=\"登录\" href=\"" + Base.BasePage.Config.WebURL + "login/login.aspx\" class=\"l_c00\">登录</a>][<a title=\"提交企业信息\" href=\"" + Base.BasePage.Config.WebURL + "member/index.aspx\" class=\"l_c00\">提交企业信息</a>]");
            strBuilder.Append("<a title=\"帮助中心\" href=\"#\">帮助中心</a>");
            strBuilder.Append("</p>");
        }
        strBuilder.Append("</div></div>");
        _headStr = strBuilder.ToString();
    }
}
